package com.example.nettyclient.ui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.nettyclient.R;
import com.example.nettyclient.base.BaseFragment;
import com.example.nettyclient.databinding.FragmentLoginBinding;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment {
    FragmentLoginBinding fragmentLoginBinding;
    String userName;
    String passWord;

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_login, container, false);
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        fragmentLoginBinding = FragmentLoginBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        fragmentLoginBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                userName = fragmentLoginBinding.userName.getText().toString();
//                passWord = fragmentLoginBinding.password.getText().toString();
//                mSharedViewModel.mNettyTcpClient.getValue().sendMsgToServer(userName + " " + passWord);
//
//                if(mSharedViewModel.loginState.getValue()){
//                    NavController controller = Navigation.findNavController(v);
//                    controller.navigate(R.id.action_loginFragment_to_mainFragment);
//                }

//                fragmentLoginBinding.progressBar.setVisibility(View.VISIBLE);


                userName = fragmentLoginBinding.userName.getText().toString();
                passWord = fragmentLoginBinding.password.getText().toString();
                if(userName.equals("root") && passWord.equals("123456")){
//                    NavController controller = Navigation.findNavController(v);
//                    controller.navigate(R.id.action_loginFragment_to_mainFragment);
                    fragmentLoginBinding.progressBar.setVisibility(View.VISIBLE);
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(2000);
                                mSharedViewModel.loginState.postValue(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    thread.start();
                }
            }
        });


        mSharedViewModel.loginState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    NavController controller = Navigation.findNavController(getView());
                    controller.navigate(R.id.action_loginFragment_to_mainFragment);
                }
            }
        });
    }
}
